<template>
  <div class="app-container">
    <div class="app-form">
      <ele-form
        v-model="formData"
        v-bind="formConfig"
        :request-fn="handleRequest"
        @request-success="handleRequestSuccess"
      />
    </div>
  </div>
</template>
<script>
import {
  typeCreate
} from '@projectName/views/diandi_place/api/room/type'
import {
  serverList
} from '@projectName/views/diandi_place/api/place/server'
import {
  getEnums
} from '@projectName/views/diandi_place/api/place/brand'
import {
  tierList
} from '@projectName/views/diandi_place/api/place/tier'
import { enumsInit } from '../../api/Enums'
export default {
  data() {
    return {
      showNum: 1,
      useHourseLis: [],
      houseName: '',
      giveHouse: false,
      formData: {
        type: 1,
        enable_week: 1,
        use_hourse_name: '',
        use_hourse: 0,
        meal_type: 1
      },
      formConfig: {
        formDesc: {
          blocs: {
            type: 'cascader-bloc-store',
            label: '选择楼栋'
          },
          title: {
            type: 'input',
            label: '房型名称'
          },
          room_type: {
            label: '类别',
            // 只需要在这里指定为 tree-select 即可
            type: 'tree-select',
            // 属性参考: https://vue-treeselect.js.org/
            attrs: {
              multiple: false,
              clearable: true
            },
            vif: (data) => data.blocs,
            optionsLinkageFields: ['blocs'],
            options: async data => {
              const res = await enumsInit()
              console.log('检索房型结果', res.data.RoomTypeEnums)

              return res.data.RoomTypeEnums
            }
          },
          displayorder: {
            type: 'input',
            label: '排序',
            attrs: {
              type: 'number'
            }
          },
          area: {
            type: 'number',
            label: '面积',
            attrs: {
              precision: 2
            }
          },
          breakfast: {
            type: 'radio',
            label: '早餐',
            isOptions: true,
            options: [{
              text: '无早',
              value: 0
            },
            {
              text: '单早',
              value: 1
            },
            {
              text: '双早',
              value: 2
            }
            ],
            default: 1
          },
          free_cancel: {
            type: 'radio',
            label: '是否免费取消',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          area_show: {
            type: 'radio',
            label: '是否显示具体位置',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          bed: {
            type: 'number',
            label: '床位数',
            attrs: {
              type: 'number'
            }
          },
          bed_show: {
            type: 'radio',
            label: '是否显示添加床位',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          bedadd: {
            type: 'radio',
            label: '是否可加床',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          checkin: {
            label: '入住时间',
            type: 'timerange',
            attrs: {
              format: 'HH:mm:ss',
              // 选择时间获取的时间格式
              valueFormat: 'HH:mm:ss'
            }
          },
          servers: {
            type: 'tree-select',
            label: '服务设施',
            vif: (data) => data.blocs,
            options: async(data) => {
              if (data.blocs[0]) {
                const res = await serverList({
                  f_bloc_id: data.blocs[0]
                })
                const arr = [{
                  id: 0,
                  label: '一级菜单'
                }]
                return arr.concat(res.data)
              }
            },
            attrs: {
              multiple: true
            }
          },
          floor_show: {
            type: 'radio',
            label: '是否显示楼层',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          isshow: {
            type: 'radio',
            label: '是否显示',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          language: {
            type: 'select',
            label: '语言类型标志/默认中文',
            options: async(data) => {
              const list = await getEnums()
              console.log('语言类型标志', list)
              const arr = []
              return arr.concat(list.data.LanguageEnums)
            }
          },
          cprice: {
            type: 'number',
            label: '现价',
            attrs: {
              precision: 2
            }
          },
          mprice: {
            type: 'number',
            label: '会员价',
            attrs: {
              precision: 2
            }
          },
          oprice: {
            type: 'number',
            label: '原价',
            attrs: {
              precision: 2
            }
          },
          cancel: {
            type: 'timerange',
            label: '取消时间',
            attrs: {
              format: 'HH:mm:ss',
              valueFormat: 'HH:mm:ss'
            }
          },
          persons: {
            type: 'input',
            label: '最多容纳人数',
            attrs: {
              type: 'number'
            }
          },
          persons_show: {
            type: 'radio',
            label: '是否显示添加人数',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          room_num: {
            type: 'input',
            label: '几室',
            attrs: {
              type: 'number'
            }
          },
          sales: {
            type: 'input',
            label: '销售数量',
            attrs: {
              type: 'number'
            }
          },
          score: {
            type: 'input',
            label: '订房积分',
            attrs: {
              type: 'number'
            }
          },
          smoke_show: {
            type: 'radio',
            label: '是否显示抽烟',
            isOptions: true,
            options: [{
              text: '是',
              value: 1
            },
            {
              text: '否',
              value: 0
            }
            ],
            default: 1
          },
          // type_id: {
          //   type: 'radio',
          //   label: '业务类型',
          //   isOptions: true,
          //   options: async(data) => {
          //     const list = await getEnums()
          //     console.log('语言类型标志', list)
          //     const arr = []
          //     return arr.concat(list.data.PlaceTypeEnums)
          //   }
          // },
          thumb: {
            type: 'image-uploader',
            label: '房型主图'
          },
          thumbs: {
            'label': '房型相册',
            'type': 'image-uploader',
            'attrs': {
              'multiple': true,
              'limit': 3
            }
          },
          toilet_num: {
            type: 'input',
            label: '几卫',
            attrs: {
              type: 'number'
            }
          }
        },
        order: ['blocs', 'title', 'room_type', 'thumb', 'thumbs', 'checkin', 'cancel', 'mprice', 'oprice', 'cprice', 'area', 'bed', 'breakfast', 'persons_show', 'smoke_show', 'bed_show', 'isshow', 'area_show', 'floor_show', 'free_cancel', 'bedadd', 'language', 'persons', 'room_num', 'toilet_num', 'sales', 'score']
      }
    }
  },
  methods: {
    handleRequest(data) {
      data.bloc_id = data.blocs[0]
      data.store_id = data.blocs[1]
      data.checkin_start = data.checkin[0]
      data.checkin_end = data.checkin[1]
      data.cancel_start = data.cancel[0]
      data.cancel_end = data.cancel[1]

      typeCreate(data).then((response) => {
        if (response.code === 200) {
          this.$message.success(response.message)
          this.closePage()
        } else {
          this.$message.error(response.message)
        }
      })
      return Promise.resolve()
    },
    handleRequestSuccess() {},
    showgiveCoupon() {
      this.giveHouse = true
    },
    selsectHouse(row) {
      console.log(row)
      const titles = []
      const giveCouponIds = []
      row.forEach((row, index) => {
        giveCouponIds.push(row.id)
        titles.push(row.name)
      })
      this.houseName = titles.join(',')
      this.formData.use_hourse = giveCouponIds.join(',')
      this.giveHouse = false
    },
    closePage() {
      this.$store.dispatch('app/closePage', {
        vm: this,
        fromName: this.$route.name,
        toName: 'diandi-place-room-type-index',
        params: {}
      })
    }
  }
}
</script>

